/*
    Copyright 2010 Farent-Solutions Ltd.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/
package com.farent.solutions.plugins.aribaweb.editor.awl;


import java.io.IOException;
import org.netbeans.core.spi.multiview.MultiViewDescription;
import org.netbeans.core.spi.multiview.MultiViewFactory;
import org.openide.cookies.EditCookie;
import org.openide.cookies.EditorCookie;
import org.openide.cookies.OpenCookie;
import org.openide.cookies.SaveCookie;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.text.CloneableEditorSupport;
import org.openide.text.DataEditorSupport;
import org.openide.windows.CloneableTopComponent;

/**
 *
 * @author farouka
 */
public class AWLEditorSupport  extends DataEditorSupport implements OpenCookie, EditorCookie, EditCookie {

    final MultiViewDescription[] descriptions = {/**new AWLMultiviewDescription1(),*/new AWLTextView(this)};

    public AWLEditorSupport(AWLDataObject obj) {
        super(obj, new AWLEnv(obj));
    }


    public static AWLEditorSupport create(AWLDataObject obj){
        return new AWLEditorSupport(obj);
    }

    @Override
    public CloneableEditorSupport.Pane createPane(){
        CloneableTopComponent tc = MultiViewFactory.createCloneableMultiView(descriptions, descriptions[0]);
        FileObject fo = getDataObject().getPrimaryFile();
        tc.setDisplayName(fo.getNameExt());
//        tc.setDisplayName("createPane.awl");
        return (Pane) tc;
    }

    @Override
    protected boolean asynchronousOpen() {
        return true;
    }

    @Override
    protected boolean notifyModified() {
        boolean retvalue;
        retvalue = super.notifyModified();
        if (retvalue){
            AWLDataObject obj = (AWLDataObject) getDataObject();
            obj.ic.add(env);
        }
        return retvalue;
    }


    @Override
    protected void notifyUnmodified() {
        super.notifyUnmodified();
        AWLDataObject obj = (AWLDataObject) getDataObject();
        obj.ic.remove(env);
    }

    private static class AWLEnv extends DataEditorSupport.Env implements SaveCookie {

        public AWLEnv(DataObject obj) {
            super(obj);
        }

        @Override
        protected FileObject getFile() {
           return super.getDataObject().getPrimaryFile();
        }

        @Override
        protected FileLock takeLock() throws IOException {
            return ((AWLDataObject)super.getDataObject()).getPrimaryEntry().takeLock();
        }

        @Override
        public void save() throws IOException {
            AWLEditorSupport ed = (AWLEditorSupport) this.findCloneableOpenSupport();
            ed.saveDocument();
        }
    }
}

